package com.uspacex.calculate.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uspacex.calculate.entity.requstClass;
import com.uspacex.calculate.entity.sourceItem;
import com.uspacex.calculate.service.createSubjectTable;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
public class UserController {

    createSubjectTable subjectTable = new createSubjectTable();

    public int sourceCount = 0;


    @RequestMapping("/precalculate")
    public String preTool(@RequestBody String str) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        System.out.println(str);
        Map<String, String> map = JSONObject.parseObject(str, Map.class);
        subjectTable.targetConfig(map);
        ArrayList<Map<String, String>> subjectItem = new ArrayList<Map<String, String>>();
        Map<String, Object> resquestParams = JSONObject.parseObject(str, Map.class);
        subjectTable.preCalculate(resquestParams, str, map);
        return "true";

    }

    @RequestMapping("/run")
    public Map<String, String> calculate(@RequestBody String str) throws Exception {
        Map<String, Object> resquestParams = JSONObject.parseObject(str, Map.class);
        Map<String, String> res = new HashMap<>();
        getConfig(resquestParams, str);
        res.put("result", "true");
        return res;

    }

    @RequestMapping(value = "/getuuid", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> getuuid(@RequestBody String str) throws Exception {
        Map<String, Object> resquestParams = JSONObject.parseObject(str, Map.class);
        Map<String, String> res = new HashMap<>();
        String s = UUID.randomUUID().toString();//用来生成数据库的主键id非常不错..
        res.put("result", s);
        return res;
    }

    @RequestMapping(value = "/getsourcedescription", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, List<String>> sourcedescription(@RequestBody String str) throws Exception {
        List<String> description = new ArrayList<>(); //=["李佳洁","测试","数据表"]
        Map<String, List<String>> result = new HashMap<>();
        description = subjectTable.getSourceDescription();
        sourceCount = description.size();
        System.out.println(sourceCount);
        //description.add("李佳洁");
        //description.add("测试");
        //description.add("数据表格");
        result.put("result", description);
        return result;
    }


    @RequestMapping(value = "/getsourceid", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, List<String>> sourceid(@RequestBody String str) {
        Map<String, List<String>> result = new HashMap<>();
        List<String> sourceId = new ArrayList<>();
        System.out.println(sourceCount);
        for (int j = 1; j <= sourceCount; j++) {
            String num = String.valueOf(j);
            sourceId.add(num);
        }
        result.put("result", sourceId);
        System.out.println(sourceId);
        return result;
    }

    @RequestMapping(value = "/gettargetdescription", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, List<String>> targetdescription(@RequestBody String str) throws Exception {
        List<String> description = new ArrayList<>(); //=["李佳洁","测试","数据表"]
        Map<String, List<String>> result = new HashMap<>();
        description = subjectTable.getTargetDescription();
        result.put("result", description);
        return result;
    }

    @RequestMapping(value = "/gettargetid", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, List<String>> targetId(@RequestBody String str) throws Exception {
        Map<String, List<String>> result = new HashMap<>();
        List<String> sourceId = new ArrayList<>();
        sourceId = subjectTable.getTargetDBName();
        result.put("result", sourceId);
        return result;
    }

    @RequestMapping(value = "/connect", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> connect(@RequestBody String str) throws Exception {
        //Map<String, Object> result = new HashMap<String, Object>();
        Map<String, String> sourcesTableConfig = new HashMap<>();
        Map<String, String> map = JSONObject.parseObject(str, Map.class);
        Connector connector = new Connector();
        String message = connector.connect(map.get("ip"), map.get("port"), map.get("username"), map.get("password"),
                map.get("dbName"));
        sourcesTableConfig = connector.sourceConfig(map.get("sourceId"), map.get("tableName"));
        //result.put("message", message);
        connector.disconnect();
        return sourcesTableConfig;
    }


    @RequestMapping(value = "/check", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    @ResponseBody
    public Map<String, String> checkProcess() throws Exception {
        Map<String, String> result = new HashMap<>();
        int i = 0;
        i = subjectTable.checkProcess();
        result.put("rows", String.valueOf(i));
        return result;
    }

    @RequestMapping(value = "/getTotalCount", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    @ResponseBody
    public Map<String, String> getTotalCount() throws Exception {
        Map<String, String> result = new HashMap<>();
        int i = 0;
        i = subjectTable.totalNum();
        result.put("count", String.valueOf(i));
        return result;
    }


    public void getConfig(Map<String, Object> requestParams, String json) throws Exception {
        ArrayList<Map<String, String>> requst = new ArrayList<Map<String, String>>();
        Map<String, String> subjectTableItem = new HashMap<>();
        requstClass requstclass = new requstClass();
        System.out.println(requestParams);
        if (requestParams == null) {
            System.out.println("null");
        } else {
            //List<sourceItem> data = (List<sourceItem>) requestParams.get("data");
            List<String> sourceTableName = new ArrayList<>();
            List<String> sourceSelectItem = new ArrayList<>();
            List<String> sourceSelectKeys = new ArrayList<>();
            List<String> totalFields = new ArrayList<>();
            List<sourceItem> data = JSON.parseArray(JSON.parseObject(json).getString("data"), sourceItem.class);

            String limites = (String) requestParams.get("limites");
            //requstclass.setLimites(limites);
            String targettable = (String) requestParams.get("subjectTable");
            //requstclass.setTargetTableName(targettable);
            String targetschema = (String) requestParams.get("subjectSchame");
            String tableDescription = (String) requestParams.get("subjectTableDescription");
            //requstclass.setTargetSchemName(targetschema);
            subjectTable.createTargetDB(targetschema);
            /*for (int i=0;i<data.size();i++){
                String sourceId = data.get(i).getSourceId();
                subjectTable.getTotalNum(sourceId,data.get(i));
            }*/
            for (int i = 0; i < data.size(); i++) {
                String sourceId = data.get(i).getSourceId();
                List<String> fields = data.get(i).getField();
                List<String> keys = data.get(i).getKeys();
                for (int j = 0; j < keys.size(); j++) {
                    if (fields.contains(keys.get(j))) {
                        fields.remove(keys.get(j));
                    }
                }
                totalFields.addAll(keys);
                totalFields.addAll(fields);
                String tableName = data.get(i).getSourceTable();
                sourceTableName.add(tableName);
                for (String K : keys) {
                    String currcentKeysName = tableName + K;
                    sourceSelectKeys.add(currcentKeysName);
                }
                for (String F : fields) {
                    String currcentFieldsName = tableName + F;
                    sourceSelectItem.add(currcentFieldsName);
                }
                System.out.println(fields);
                subjectTable.getSourceConfig(sourceId, data.get(i), limites, targetschema, targettable, i);
            }
            subjectTable.createSubjectTable1(targettable, sourceSelectItem, sourceSelectKeys, sourceTableName, targetschema);
            subjectTable.alterTable(targettable, tableDescription, targetschema);
        }
    }

    @RequestMapping(value = "/createSubjectTable", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> showTables(@RequestBody String str) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        System.out.println(str);
        ArrayList<Map<String, String>> subjectItem = new ArrayList<Map<String, String>>();
        Map<String, Object> resquestParams = JSONObject.parseObject(str, Map.class);
        //subjectItem = getDBConfig(resquestParams);
        if (subjectItem == null || subjectItem.isEmpty()) {
            return null;
        } else {
            //result.put("data", "");
            return resquestParams;
        }
    }


}